/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.jSkyRoom.bridge.major;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import ru.jSkyRoom.finder.FinderByFormId;
import ru.jSkyRoom.interfaces.major.Container;
import ru.jSkyRoom.interfaces.major.Major;
import ru.jSkyRoom.interfaces.stub.JSkySound;
import ru.jSkyRoom.interfaces.sub.JSkyModel;
import skyproc.CONT;
import skyproc.FormID;
import skyproc.ItemListing;
import skyproc.Model;

/**
 *
 * @author Rakev
 */
public class ContainerBridge extends NamedMajorBridge<CONT> implements Container {

    public ContainerBridge(CONT adapting) {
        super(adapting);
    }

    @Override
    public JSkyModel getModel() {
        Model model = getRawObject().getModelData();
        return getBridgeFactory().createBridge(model);
    }

    @Override
    public Map<Major, Integer> getItems() {
        List<ItemListing> rawItems = getRawObject().getItems();
        Map<Major, Integer> result = new HashMap<>(rawItems.size());
        FinderByFormId<Major> finder = getFinderFactory().createMajorFinder();

        for (ItemListing rawItem : rawItems) {
            Major item = finder.find(rawItem.getForm());
            result.put(item, rawItem.getCount());
        }
        return Collections.unmodifiableMap(result);
    }

    @Override
    public void putItem(Major item, int count) {
        FormID itemID = item.getForm();
        getRawObject().addItem(itemID, count);
    }

    @Override
    public void removeItem(Major item) {
        FormID id = getIdProducer().produce(item);
        getRawObject().removeItem(id);
    }

    @Override
    public JSkySound getOpenSound() {
        FormID soundId = getRawObject().getOpenSound();
        return getFinderFactory().createJSkySoundFinder().find(soundId);
    }

    @Override
    public void setOpenSound(JSkySound sound) {
        FormID id = getIdProducer().produce(sound);
        getRawObject().setOpenSound(id);
    }

    @Override
    public JSkySound getCloseSound() {
        FormID soundId = getRawObject().getCloseSound();
        return getFinderFactory().createJSkySoundFinder().find(soundId);
    }

    @Override
    public void setCloseSound(JSkySound sound) {
        FormID id = getIdProducer().produce(sound);
        getRawObject().setCloseSound(id);
    }
}
